<template>
  <div>
    <el-row v-if="designListArr.length > 0">
      <div class="detail_header">
        <span class="title">{{ $t('caseorder.yulan.yulan81') }}</span>
      </div>
    </el-row>
    <div v-for="(item, index) in designListArr" :key="index" class="file">
      <div class="file_left">
        <img src="@/assets/img/order_file.png" alt="" />
        <div class="file_left_info">
          <span>{{ item.fileName }}</span>
          <span
            >{{ $t('caseorder.yulan.yulan82') }}：{{
              item.content || $t('caseorder.yulan.yulan83')
            }}</span
          >
        </div>
      </div>
      <div class="file_right">
        <!-- <img @click="fileChangeHandle" src="@/assets/img/file_changed.png" alt=""> -->
        <!-- <img src="@/assets/img/file_noChange.png" alt=""> -->
        <img
          src="@/assets/img/file_download.png"
          alt=""
          @click="download(item.fileUrl, item.fileName)"
        />
      </div>
    </div>
    <FileChange ref="fileChange" />
  </div>
</template>

<script>
import FileChange from '@/components/filechange'; //
export default {
  // 下载
  name: 'BLZMasterDownload',
  components: {
    FileChange,
  },
  data() {
    return {
      designListArr: [],
    };
  },
  // props: ['fileListArr'],
  created() {
    this.getSelectDesignList();
  },

  methods: {
    fileChangeHandle() {
      this.$refs.fileChange.dialogShow = true;
    },
    download(url, name) {
      // this.downloadIamge(url, name)
      window.open(url);
      // window.location.href = url
    },
    downloadIamge(imgsrc, name) {
      // 下载图片地址和图片名
      var image = new Image();
      console.dir(image);
      // 解决跨域 Canvas 污染问题
      image.crossOrigin = 'anonymous';
      // image.setAttribute("crossOrigin", "anonymous");
      image.onload = function () {
        var canvas = document.createElement('canvas');
        canvas.width = image.width;
        canvas.height = image.height;
        var context = canvas.getContext('2d');
        context.drawImage(image, 0, 0, image.width, image.height);
        var url = canvas.toDataURL('image/png'); // 得到图片的base64编码数据

        var a = document.createElement('a'); // 生成一个a元素
        var event = new MouseEvent('click'); // 创建一个单击事件
        a.download = name || 'photo'; // 设置图片名称
        a.href = url; // 将生成的URL设置为a.href属性
        a.dispatchEvent(event); // 触发a的单击事件
      };
      // image.setAttribute("crossOrigin", "anonymous");
      image.src = imgsrc;
    },
    download2(imgsrc) {
      return new Promise((resolve, reject) => {
        try {
          var url = '';
          var image = new Image();
          console.dir(image);

          // 解决跨域 Canvas 污染问题
          // image.setAttribute("crossOrigin", "anonymous");
          image.onload = function () {
            var canvas = document.createElement('canvas');
            canvas.width = image.width;
            canvas.height = image.height;
            var context = canvas.getContext('2d');
            context.drawImage(image, 0, 0, image.width, image.height);
            url = canvas.toDataURL('image/png'); // 得到图片的base64编码数据
            resolve({
              url: url,
              width: canvas.width,
              height: canvas.height,
            });
          };
          image.crossOrigin = 'Anonymous';
          image.src = imgsrc;
        } catch (e) {
          reject();
        }
      });
    },
    getSelectDesignList() {
      this.$http
        .post('/order/selectDesignList', { orderId: this.$route.query.id })
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          } else {
            this.designListArr = [];
            this.designListArr = res.data;
          }
        })
        .catch((err) => {
          console.log('err', err);
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.detail_header {
  display: flex;
  align-items: center;
  margin-bottom: 12px;

  .title {
    font-size: 18px;
    font-weight: 500;
    line-height: 20px;
    color: #003e67;
  }
}

.file {
  margin-bottom: 10px;
  background-color: #f5f5f5;
  padding: 10px 30px 10px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .file_left {
    display: flex;

    img {
      width: 22px;
      height: 30px;
    }

    .file_left_info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      margin-left: 12px;

      span {
        font-size: 12px;
        font-weight: 500;
        line-height: 11px;
        color: #333333;
      }
    }
  }

  .file_right {
    img {
      cursor: pointer;
      width: 16px;
      height: 16px;
    }

    img:nth-child(2) {
      margin-left: 20px;
    }
  }
}
</style>
